<?php


namespace app\job;

use app\common\model\order\ShopOrderModel;
use app\common\service\order\ShopOrderLogService;
use app\common\service\order\ShopOrderService;
use app\common\service\webscoket\CacheRedisService;
use think\queue\Job;
use think\facade\Log;

/**
 * 订单上架队列
 */
class OrderScheduleJob
{
    public function fire(Job $job, $data)
    {
        try {
            foreach ($data as $key=>$val){
                //判断是否为买家自选订单，是：状态更改为待自选，否：待接单
                $status = (isset($val['is_self_selection']) && $val['is_self_selection']==1)?ShopOrderModel::getStatus('o_select',1):ShopOrderModel::getStatus('o_taking',1);
                $edit_data = [
                    'status'=>$status,
                    'old_status'=>array_key_exists('old_status', $val)?$val['old_status']:ShopOrderModel::getStatus('o_listing',1),
                    'advertising_time'=>date('Y-m-d H:i:s')
                ];
                $where = [
                    ['id','=',$val['id']],
                    ['status','=',ShopOrderModel::getStatus('o_listing',1)],
                ];
                $eidt_res = ShopOrderService::customEdit($where,$edit_data);
                if($eidt_res){
                    //删除缓存
                    CacheRedisService::sRem('OrderScheduleListener',$val['id']);
                    $log_res = ShopOrderLogService::addByCode('DDSJ',[
                        'merchant_id'=>$val['merchant_id'],
                        'shop_order_id'=>$val['id'],
                        'order_status'=>$edit_data['status'],
                    ]);
                }
            }
        } catch (\Exception $e) {
            Log::error("订单上架报错：".print_r($data,true)."内容：".$e->getMessage());
        }
        $job->delete();
    }
}
